/**
 * 短信息功能JS文件
 * 
 * @author		布衣才子
 * @date		2012-10-04
 * @email		work.jerryliu@gmail.com
 * @qq			394969553
 * @version		v1.0
 * @copyright	copyright 2012-2014	YeeYi.com All Rights Reserved	
 */

/**
 * 
 * 服务器访问地址
 */
var apiHost = "http://www.yeeyi.com/bbs/forum.php?jsoncallback=?&mod=mobile";

/**
 * 用于分页
 */
var messagePage = 1;
var b64 = new Base64();

/**
 * 网络访问函数
 * @param {Object} url	请求的网络地址
 * @param {Object} callback	回调函数
 */
function xmlHttp(url,callback){
	if(url == ''){
		uexWindow.alert('参数错误','请求地址不能为空！','返回');
	}else{
		var platform = getLocalStorge('platform');
		url = url + "&pf="+platform;
		$.getJSON(url,callback);
	}
}

/**
 * 获取短信息列表
 */
function getMessageList(){
	//uexWindow.toast("0","5","内容加载中…","0");
	//uexWindow.toast("1","5","内容加载中…","0");
	var uid = getLocalStorge('uid');
	
	if(!uid){
		//uexWindow.closeToast();
		alert('获取失败，请先登录！');
	}
	
	messagePage = 1;
	var url = apiHost + '&ac=message&type=list&uid='+uid;
	xmlHttp(url,showList);
}

/**
 * 获取短信息的回调函数
 * @param {Object} items
 */
function showList(items){
	var returnCode = items[0].code;
	//uexWindow.closeToast();
	if(returnCode == '404'){
		//uexWindow.toast("0","5","加载失败，用户信息丢失，请先登录…","2000");
		alert("加载失败，用户信息丢失，请先登录");
		return false;
	}
	var messageObj = $("#forum_list");
	messageObj.html('');
	var j = 0;
	
	for(var i in items){
		if(i > 0){
			j = j + 1;
			var item = items[j];
			var tr = '';
			var trHeader = '<div ontouchstart="zy_touch(\'btn-act\')" class="forumList" title="'+item.pmid+'"><div class="ub-f1 ub ub-ver">';
			var trOtherInfo = '<div class="otherInfo"><span>状态:'+b64.decode(item.isread)+'</span><span style="margin-left:1em;">时间:'+b64.decode(item.dateline)+'</span> <span style="margin-left:1em;"> 来源: '+b64.decode(item.msgfrom)+'</span></div>';
			var trDesc = '<div class="desc"><div class="subject" onclick="showDetail('+item.pmid+')">'+b64.decode(item.subject)+'</div>';
			var detail = '<div class="detail"></div>';
			var trFooter = '</div></div>';
			tr = trHeader+trOtherInfo+trDesc+detail+trFooter;
			messageObj.append(tr);
		}
	}
	
	if(j == 0){
		var tr = '';
		var trHeader = '<div ontouchstart="zy_touch(\'btn-act\')" class="forumList"><div class="ub-f1 ub ub-ver">';
		var trOtherInfo = '<div class="otherInfo"><span>提示:</span></div>';
		var trDesc = '<div class="desc">当前没有短消息!</div>';
		var trFooter = '</div></div>';
		tr = trHeader+trOtherInfo+trDesc+trFooter;
		messageObj.append(tr);
	}
	
	if(j == 10){
		$('.footer').removeClass('footerHide');
	}
}

/**
 * 显示下一页短信息
 * @param {Object} items
 */
function showMoreMessage(items){
	var messageObj = $("#forum_list");
	var j = 0;
	for(var i in items){
		if(i > 0){
			j = j + 1;
			var item = items[j];
			var tr = '';
			var trHeader = '<div ontouchstart="zy_touch(\'btn-act\')" class="forumList" title="'+item.pmid+'"><div class="ub-f1 ub ub-ver">';
			var trOtherInfo = '<div class="otherInfo"><span>状态:'+b64.decode(item.isread)+'</span><span style="margin-left:1em;">时间:'+b64.decode(item.dateline)+'</span> <span style="margin-left:1em;"> 来源: '+b64.decode(item.msgfrom)+'</span></div>';
			var trDesc = '<div class="desc"><div class="subject" onclick="showDetail('+item.pmid+')">'+b64.decode(item.subject)+'</div>';
			var detail = '<div class="detail"></div>';
			var trFooter = '</div></div>';
			tr = trHeader+trOtherInfo+trDesc+trFooter;
			messageObj.append(tr);
		}
	}
	uexWindow.closeToast();
	if(j == 10){
		$("#nextpage").text("查看下10条");
	}else{
		$("#nextpage").text("已经全部显示");
	}
	
}

/**
 * 获取下一页短信息
 */
function getMoreMessage(){
	var uid = getLocalStorge('uid');
	messagePage = messagePage + 1;
	var url = apiHost + '&ac=message&type=list&uid='+uid+'&page='+messagePage;
	
	$("#nextpage").text("数据加载中……");
	xmlHttp(url,showMoreMessage);
}

/**
 * 显示短信息详情
 * @param {Object} pmid
 */
function showDetail(pmid){
	if(pmid){
		uexWindow.toast("1","5","内容加载中…","0");
		localStorage.setItem('pmid',pmid);
		var uid = getLocalStorge('uid');
		var password = getLocalStorge('pwd');
		if(!uid){
			uexWindow.closeToast();
			uexWindow.toast("0","5","用户信息已过期，请重新登录…","2000");
		}else{
			var url = apiHost + '&ac=message&type=show&pmid='+pmid+'&uid='+uid+'&pwd='+password;
			xmlHttp(url,showMess);
		}
	}else{
		uexWindow.toast("0","5","显示失败，参数丢失…","2000");
	}
}

/**
 * 显示短信的回调函数
 * @param {Object} data
 */
function showMess(data){
	var returnCode = data[0].code;
	uexWindow.closeToast();
	if(returnCode == '403'){
		uexWindow.toast("0","5","加载失败，用户信息丢失，请先登录…","2000");
		return false;
	}else if(returnCode == '404'){
		uexWindow.toast("0","5","加载失败，短信参数缺失…","2000");
		return false;
	}else if(returnCode == '200'){
		var returnData = data[0].message;
		var pmid = getLocalStorge('pmid');
		if(!pmid){
			uexWindow.toast("0","5","加载失败，短信参数丢失…","2000");
		}else{
			uexWindow.toast("0","5","数据加载成功…","2000");
			$('.forumList[title="'+pmid+'"] .subject').hide();
			$('.forumList[title="'+pmid+'"] .detail').html(b64.decode(returnData)).show('slow');
			//$('.forumList[title="'+pmid+'"] .reply').show();
		}
	}else{
		uexWindow.toast("0","5","加载失败，未知错误…","2000");
		return false;
	}
}

function reply(pmid){
	var uid = getLocalStorge('uid');
	var password = getLocalStorge('pwd');
	if(!pmid){
		uexWindow.toast("0","5","回复失败，短信参数丢失…","2000");
		return false;
	}
	
	if(!uid){
		uexWindow.toast("0","5","回复失败，请重新登录…","2000");
		return false;
	}
	
	$('.forumList[title="'+pmid+'"] .replyButton').html('回复中…');
	var replyMessage = $('#m_'+pmid).val();
	if(replyMessage.length < 1){
		uexWindow.toast("0","5","回复失败，内容过短…","2000");
		return false;
	}
	
	var requestURL = apiHost + "&ac=message&type=reply&pmid="+pmid+"&uid="+uid+"&pwd="+password;
	xmlHttp(requestURL,showReply);
}

function showReply(data){	
}

/**
 * 关闭窗口
 */
function closeWindow(){
    var closeString = "uexWindow.close(-1);";
    uexWindow.evaluateScript('', '0', closeString);
}

/**
 * HTML5 localStorge.getItem 函数的封装
 * @param {Object} key
 */
function getLocalStorge(key){
	var item = localStorage.getItem(key);
	if(item=='' || item==null || typeof(item)=="undefined"){
		return false;
	}else{
		return item;
	}
}